package com.glory.hikvisionmobile.dialog;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
import androidx.fragment.app.FragmentManager;

import com.glory.hikvisionmobile.R;

import butterknife.BindView;

/**
 * *****************************
 *
 * @Anthor yazhou
 * @Email 1184545990@qq.com
 * @Time 2020/4/2 16:22
 * @DESC *****************************
 */
public class MaterialCountInputDialog extends DialogFragment {

    private OnConfirmListener onConfirmListener;

    EditText etCount;
    TextView tvConfirm;
    TextView tvCancel;


    public static void show(FragmentManager manager, OnConfirmListener onConfirmListener) {
        MaterialCountInputDialog materialCountInputDialog = new MaterialCountInputDialog();
        materialCountInputDialog.setOnConfirmListener(onConfirmListener);
        materialCountInputDialog.show(manager, "MaterialCountInputDialog");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.dialog_material_count_input, container, false);
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        etCount = view.findViewById(R.id.etCount);
        tvConfirm = view.findViewById(R.id.tvConfirm);
        tvCancel = view.findViewById(R.id.tvCancel);
        tvConfirm.setOnClickListener(v -> {
            dismiss();
            if (onConfirmListener != null) {
                int count = 0;
                try {
                    count = Integer.valueOf(etCount.getText().toString().trim());
                } catch (NumberFormatException e) {

                }
                onConfirmListener.onConfirm(count);
            }
        });
        tvCancel.setOnClickListener(v -> {
            dismiss();
        });
    }

    public void setOnConfirmListener(OnConfirmListener onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    @Override
    public void onDismiss(@NonNull DialogInterface dialog) {
        super.onDismiss(dialog);
    }

    public interface OnConfirmListener {
        void onConfirm(int count);
    }
}
